/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import company.search.interfaces.SearchRequerimetsOR;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author damfercer
 */
@Entity
@Table(name = "search_language_info_requeriment_or_operator")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "SearchLanguageInfoRequerimentOrOperator.findAll", query = "SELECT s FROM SearchLanguageInfoRequerimentOrOperator s"),
    @NamedQuery(name = "SearchLanguageInfoRequerimentOrOperator.findByIdSearchLanguageInfoRequerimentOrOperator", query = "SELECT s FROM SearchLanguageInfoRequerimentOrOperator s WHERE s.idSearchLanguageInfoRequerimentOrOperator = :idSearchLanguageInfoRequerimentOrOperator"),
    @NamedQuery(name = "SearchLanguageInfoRequerimentOrOperator.findByRequiredVideo", query = "SELECT s FROM SearchLanguageInfoRequerimentOrOperator s WHERE s.requiredVideo = :requiredVideo")})
public class SearchLanguageInfoRequerimentOrOperator implements Serializable, SearchRequerimetsOR {
    @JoinColumn(name = "WRITING_LEVEL", referencedColumnName = "ID_LANGUAGE_LEVEL")
    @ManyToOne
    private LanguageLevel writingLevel;
    @Basic(optional = false)
    @NotNull
    @Column(name = "IMPORTANCE")
    private int importance;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID_SEARCH_LANGUAGE_INFO_REQUERIMENT_OR_OPERATOR")
    private Integer idSearchLanguageInfoRequerimentOrOperator;
    @Column(name = "REQUIRED_VIDEO")
    private Boolean requiredVideo;
    @JoinColumn(name = "LANGUAGE", referencedColumnName = "ID_LANGUAGE")
    @ManyToOne(fetch = FetchType.LAZY)
    private Language language;
    @JoinColumn(name = "SEARCH", referencedColumnName = "ID_SEARCH")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Search search;
    @JoinColumn(name = "MINIMUM_LANGUAGE_LEVEL", referencedColumnName = "ID_LANGUAGE_LEVEL")
    @ManyToOne(fetch = FetchType.LAZY)
    private LanguageLevel minimumLanguageLevel;

    public SearchLanguageInfoRequerimentOrOperator() {
    }

    public SearchLanguageInfoRequerimentOrOperator(Integer idSearchLanguageInfoRequerimentOrOperator) {
        this.idSearchLanguageInfoRequerimentOrOperator = idSearchLanguageInfoRequerimentOrOperator;
    }

    public Integer getIdSearchLanguageInfoRequerimentOrOperator() {
        return idSearchLanguageInfoRequerimentOrOperator;
    }

    public void setIdSearchLanguageInfoRequerimentOrOperator(Integer idSearchLanguageInfoRequerimentOrOperator) {
        this.idSearchLanguageInfoRequerimentOrOperator = idSearchLanguageInfoRequerimentOrOperator;
    }

    public Boolean getRequiredVideo() {
        return requiredVideo;
    }

    public void setRequiredVideo(Boolean requiredVideo) {
        this.requiredVideo = requiredVideo;
    }

    public Language getLanguage() {
        return language;
    }

    public void setLanguage(Language language) {
        this.language = language;
    }

    public Search getSearch() {
        return search;
    }

    public void setSearch(Search search) {
        this.search = search;
    }

    public LanguageLevel getMinimumLanguageLevel() {
        return minimumLanguageLevel;
    }

    public void setMinimumLanguageLevel(LanguageLevel minimumLanguageLevel) {
        this.minimumLanguageLevel = minimumLanguageLevel;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idSearchLanguageInfoRequerimentOrOperator != null ? idSearchLanguageInfoRequerimentOrOperator.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        boolean languageRes=false;
        boolean minimumLanguageLevelRes=false;
        boolean requiredVideoRes=false;
        if (!(object instanceof SearchLanguageInfoRequerimentOrOperator)) {
            return false;
        }
        SearchLanguageInfoRequerimentOrOperator other = (SearchLanguageInfoRequerimentOrOperator) object;
        
        //Language test
        if(this.language==null && other.getLanguage()==null){
            languageRes=true;
        }
        else if(this.language!=null && other.getLanguage()!=null){
            languageRes=this.language.equals(other.getLanguage());
        }
        //MinimumLanguageLevel test
        if(this.getMinimumLanguageLevel()==null && other.getMinimumLanguageLevel()==null){
            minimumLanguageLevelRes=true;
        }
        else if(this.getMinimumLanguageLevel()!=null && other.getMinimumLanguageLevel()!=null){
            minimumLanguageLevelRes=this.getMinimumLanguageLevel().equals(other.getMinimumLanguageLevel());
        }
        //RequiredVideo test
        if(this.getRequiredVideo()==null && other.getRequiredVideo()==null){
            requiredVideoRes=true;
        }
        else if(this.getRequiredVideo()!=null && other.getRequiredVideo()!=null){
            requiredVideoRes=this.getRequiredVideo().equals(other.getRequiredVideo());
        }
        Boolean res=languageRes && minimumLanguageLevelRes && requiredVideoRes;
        return res;
        
    }

    @Override
    public String toString() {
        return "entities.SearchLanguageInfoRequerimentOrOperator[ idSearchLanguageInfoRequerimentOrOperator=" + idSearchLanguageInfoRequerimentOrOperator + " ]";
    }

    public int getImportance() {
        return importance;
    }

    public void setImportance(int importance) {
        this.importance = importance;
    }

    
    public LanguageLevel getWritingLevel() {
        return writingLevel;
    }

    public void setWritingLevel(LanguageLevel writingLevel) {
        this.writingLevel = writingLevel;
    }
    
    @Override
    public Integer getCandidateTotalPoints(Candidate c) {
        Integer finalPoints=0;
        Integer points;
        Integer languagePoints=40;
        Integer videoPoints=40;
        Integer speakLanguageLevelPoints=20;
        Integer writingLangugageLevelPoints=20;
        Integer requeriments=0;
        Integer candidateRequeriments;
        if(c.getCandidateLanguageInfoList()!=null && !c.getCandidateLanguageInfoList().isEmpty()){
            if(getLanguage()!=null){
                requeriments++;
            }
            if(getMinimumLanguageLevel()!=null){
                requeriments++;
            }
            if(getWritingLevel()!=null){
                requeriments++;
            }
            if(getRequiredVideo()!=null){
                requeriments++;
            }
           
            for(CandidateLanguageInfo candidateLanguageInfo:c.getCandidateLanguageInfoList()){
                candidateRequeriments=0;
                points=0;
                if(getLanguage()!=null){
                    if(candidateLanguageInfo.getLanguage()!=null && candidateLanguageInfo.getLanguage().equals(getLanguage()) ){
                        points+=(languagePoints*importance);
                        candidateRequeriments++;
                    }
                }
                if(getRequiredVideo()!=null){
                    if(candidateLanguageInfo.getVideo()!=null){
                        points+=(videoPoints*importance);
                        candidateRequeriments++;
                    }
                }
                if(getMinimumLanguageLevel()!=null){
                    if(candidateLanguageInfo.getLanguageLevel()!=null && candidateLanguageInfo.getLanguageLevel().getValue()>=getMinimumLanguageLevel().getValue()){
                        points+=(speakLanguageLevelPoints*importance);
                        candidateRequeriments++;
                    }
                }
                if(getWritingLevel()!=null){
                    if(candidateLanguageInfo.getWritingLevel()!=null && candidateLanguageInfo.getWritingLevel().getValue()>=getWritingLevel().getValue()){
                        points+=(writingLangugageLevelPoints*importance);
                    }
                }
                if(requeriments<=candidateRequeriments){
                    points=points*2;
                }
                finalPoints+=points;
            }
        }
        return finalPoints;
    }

}
